#include "ioManager.h"
#include "viewport.h"
#include <cmath>;

Viewport* Viewport::getInstance() {
  if (!instance) instance = new Viewport;
  return instance;
}

Viewport::Viewport() :
  frozen(false),
  gdata(Gamedata::getInstance()),
  position(0, 0),
  viewWidth(gdata->getXmlInt("viewWidth")), 
  viewHeight(gdata->getXmlInt("viewHeight")),
  worldWidth(gdata->getXmlInt("worldWidth")),
  worldHeight(gdata->getXmlInt("worldHeight")),
  objWidth(gdata->getXmlInt("stepWidth")), objHeight(gdata->getXmlInt("stepHeight")),
  height(0),
  objectToTrack(NULL) 
{}

void Viewport::draw() const {
  std::stringstream strm;
  std::string msg;

  strm << "viewport at (" << static_cast<int>( X() )
              << ", " << static_cast<int>( Y() ) << ")\0";
  msg = strm.str();

  if(height < (-1) * (static_cast<int>(objectToTrack->Y()) - 512))
	  height = (-1) * (static_cast<int>(objectToTrack->Y()) - 512);

  //IOManager::getInstance()->printMessageAt(msg.c_str(), 10, 70);
  strm.str("");
  strm << "Scores: " << height/10 << "\0";
  msg = strm.str();
  IOManager::getInstance()->printMessageAt(msg.c_str(), 10, 10);

}

void Viewport::setObjectToTrack(const Sprite *obj) { 
  objectToTrack = obj; 
  objWidth = objectToTrack->getFrame()->getWidth();
  objHeight = objectToTrack->getFrame()->getHeight();
}

void Viewport::setCurrentScreen(const Vector2f& curScreen) {
	this->currentScreen = curScreen;
}

void Viewport::update() {
	if (frozen)
		return;
	const float x = objectToTrack->X();
	const float y = objectToTrack->Y();

	//Here we must judge whether doodle's y position is higher than half the screen
	if (abs(Y() - y) < (viewHeight / 2)) {
		Y(y - (viewHeight / 2));
	}


}


  //if (position[0] < 0) position[0] = 0;
  //if (position[1] < 0) position[1] = 0;
//  if (position[0] > (worldWidth - viewWidth)) {
//    position[0] = worldWidth-viewWidth;
//  }
//  if (position[1] > (worldHeight - viewHeight)) {
//    position[1] = worldHeight-viewHeight;
// }

